"""Global rate limiter shared across routers."""

import os
import sys

# 设置环境变量，确保文件读取使用UTF-8编码
os.environ['PYTHONIOENCODING'] = 'utf-8'

# 尝试设置标准输出编码
try:
    if hasattr(sys, 'stdout'):
        sys.stdout.reconfigure(encoding='utf-8')
    if hasattr(sys, 'stderr'):
        sys.stderr.reconfigure(encoding='utf-8')
except:
    pass

from slowapi import Limiter
from slowapi.util import get_remote_address

# 创建Limiter实例时指定编码
try:
    limiter = Limiter(key_func=get_remote_address)
except UnicodeDecodeError:
    # 如果仍然有编码问题，尝试使用更简单的方法
    import warnings
    warnings.filterwarnings("ignore", category=UnicodeWarning)
    limiter = Limiter(key_func=get_remote_address)

__all__ = ["limiter"]
